package com.jf.blogbackened.controller;

import com.alibaba.fastjson.JSON;
import com.jf.blogbackened.bean.Admin;
import com.jf.blogbackened.bean.PageBean;
import com.jf.blogbackened.bean.Result;
import com.jf.blogbackened.bean.User;
import com.jf.blogbackened.service.UserService;
import com.jf.blogbackened.utils.MyJwtUtil;
import io.jsonwebtoken.Claims;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author David
 */
@Controller
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;

    @RequestMapping("/login")
    @ResponseBody
    public String login(@RequestBody Admin admin) {
        // 1 接收参数
        // 2封装实体类
        // 处理业务逻辑
        System.out.println(admin.getUsername() + admin.getPassword());
        Result<Admin> res = userService.login(admin);
        return JSON.toJSONString(res);
    }

    /**
     * 获取用户信息
     *
     * @Param token, 令牌：会将用户登录的信息转换成一串编码
     * 给予服务端，服务端根据此编码利用特定的方式进行解析，
     * 验证此信息是否合法，相当于校验功能。为了防止在通信过程中
     * 信息被第三方篡改，保证信息的时效性
     */
    @RequestMapping("/info")
    @ResponseBody
    public String getInfo(String token) {
        // 调用工具类进行转换
        Claims claims = MyJwtUtil.parseJWT(token);
        if (claims == null) {
            // 说明该令牌已过期或信息被篡改
            return JSON.toJSONString(Result.error().put("message", "您的登录已超时"));
        }
        // 如果token没过期，则获取内部的信息，本质是JSON格式的字符串
        String subject = claims.getSubject();
        System.out.println(subject);
        // 将转换过后的字符串，进行JSON解析，得到用户实体
        Admin admin = JSON.parseObject(subject, Admin.class);
        return JSON.toJSONString(Result.ok().put("data", admin));
    }

    @RequestMapping("/logout")
    @ResponseBody
    public Result logout(String token) {
        return Result.ok().put("data", token);
    }

    @GetMapping("/getAllUser")
    @ResponseBody
    public String getAllUser(Integer currentPage, Integer pageSize) {
        PageBean<User> pageBean = userService.getAllUser(new User(), currentPage, pageSize);
        return JSON.toJSONString(Result.ok().put("data", pageBean));
    }

    @GetMapping("/queryData")
    @ResponseBody
    public String queryData(User params, Integer currentPage, Integer pageSize) {
        PageBean<User> pageBean = userService.getAllUser(params, currentPage, pageSize);
        return JSON.toJSONString(Result.ok().put("data", pageBean));
    }

    @PostMapping("/addOrUpdate")
    @ResponseBody
    public String addOrUpdate(@RequestBody User user) {
        Result<User> res = userService.addOrUpdateUser(user);
        return JSON.toJSONString(res);
    }

    @GetMapping("/getUserById")
    @ResponseBody
    public String getUserById(Integer id) {
        Result<User> res = userService.getUserById(id);
        return JSON.toJSONString(res);
    }

    @GetMapping("/deletUserById")
    @ResponseBody
    public String deletUserById(Integer id) {
        Result<User> res = userService.deleteUserById(id);
        return JSON.toJSONString(res);
    }

    @GetMapping("/getAllUsers")
    @ResponseBody
    public String getAllUsers() {
        Result res = userService.getAllUsers();
        return JSON.toJSONString(res);
    }

}

